{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "359f82e4-baea-4137-b653-5ecb2fa5be1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import sys\n",
    "import os\n",
    "import json\n",
    "import cv2\n",
    "import uuid\n",
    "sys.path.append('../../../src')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b6292f40-3870-4bf4-92c4-8187ee9fc4ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sryolo.devtools import os_utils\n",
    "from sryolo.devtools import dataset_utils\n",
    "from sryolo.devtools import label_studio_utils\n",
    "from sryolo.devtools import ultralytics_utils"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7561f2f3-f882-475c-a3af-7b107c53a8d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset_name = 'full-v1'\n",
    "train_name = 'yolov8n-1088-full-v1'\n",
    "save_name = 'yolov8n-1088-full-0428'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "37586d94-a502-4dd5-b415-abeef0c27075",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ultralytics YOLOv8.1.44  Python-3.11.9 torch-2.2.2+cu121 CPU (AMD Ryzen 9 5950X 16-Core Processor)\n",
      "Model summary (fused): 168 layers, 3058283 parameters, 0 gradients, 8.3 GFLOPs\n",
      "\n",
      "\u001B[34m\u001B[1mPyTorch:\u001B[0m starting from 'F:\\code\\workspace\\StarRail-YOLO\\ultralytics\\runs\\full-v1\\yolov8n-1088-full-v1\\weights\\best.pt' with input shape (1, 3, 544, 960) BCHW and output shape(s) (1, 73, 10710) (6.3 MB)\n",
      "\n",
      "\u001B[34m\u001B[1mONNX:\u001B[0m starting export with onnx 1.16.0 opset 17...\n",
      "\u001B[34m\u001B[1mONNX:\u001B[0m export success  1.0s, saved as 'F:\\code\\workspace\\StarRail-YOLO\\ultralytics\\runs\\full-v1\\yolov8n-1088-full-v1\\weights\\best.onnx' (11.9 MB)\n",
      "\n",
      "Export complete (2.6s)\n",
      "Results saved to \u001B[1mF:\\code\\workspace\\StarRail-YOLO\\ultralytics\\runs\\full-v1\\yolov8n-1088-full-v1\\weights\u001B[0m\n",
      "Predict:         yolo predict task=detect model=F:\\code\\workspace\\StarRail-YOLO\\ultralytics\\runs\\full-v1\\yolov8n-1088-full-v1\\weights\\best.onnx imgsz=544,960  \n",
      "Validate:        yolo val task=detect model=F:\\code\\workspace\\StarRail-YOLO\\ultralytics\\runs\\full-v1\\yolov8n-1088-full-v1\\weights\\best.onnx imgsz=544,960 data=F:\\code\\workspace\\StarRail-YOLO\\ultralytics\\datasets\\full-v1\\dataset.yaml  WARNING  non-PyTorch val requires square images, 'imgsz=[544, 960]' will not work. Use export 'imgsz=960' if val is required.\n",
      "Visualize:       https://netron.app\n"
     ]
    }
   ],
   "source": [
    "ultralytics_utils.export_model(dataset_name,\n",
    "                               train_name=train_name,\n",
    "                               save_name=save_name,\n",
    "                              imgsz=(544, 960))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "dfb468f4-ae00-4b63-b37f-c04f727718ad",
   "metadata": {},
   "outputs": [],
   "source": [
    "from ultralytics import YOLO"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "0f0f80e0-d1ed-4fdb-b638-d789f5c03c05",
   "metadata": {},
   "outputs": [],
   "source": [
    "onnx_model = YOLO(os.path.join(os_utils.get_work_dir(), 'models', save_name, 'model.onnx'), task='detect')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "73c5f81d-c9fa-4595-ba1b-c7132aafb57d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ultralytics YOLOv8.1.44  Python-3.11.9 torch-2.2.2+cu121 CPU (AMD Ryzen 9 5950X 16-Core Processor)\n",
      "Loading F:\\code\\workspace\\StarRail-YOLO\\models\\yolov8n-1088-full-0428\\model.onnx for ONNX Runtime inference...\n",
      "\u001B[31m\u001B[1mrequirements:\u001B[0m Ultralytics requirement ['onnxruntime'] not found, attempting AutoUpdate...\n",
      "\u001B[31m\u001B[1mrequirements:\u001B[0m  AutoUpdate skipped (offline)\n",
      "Forcing batch=1 square inference (1,3,1088,1088) for non-PyTorch models\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001B[34m\u001B[1mval: \u001B[0mScanning F:\\code\\workspace\\StarRail-YOLO\\ultralytics\\datasets\\full-v1\\labels.cache... 85 images, 0 backgrounds, 0 corrupt: 100%|██████████| 85/85 [00:00<?, ?it/s]\u001B[0m\n",
      "                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100%|██████████| 85/85 [00:13<00:00,  6.32it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                   all         85        357      0.843      0.864      0.892      0.658\n",
      "               0001-pm         85          3          1      0.918      0.995      0.853\n",
      "               0002-xz         85          9      0.922      0.889      0.975      0.598\n",
      "               0003-gz         85          7      0.928          1      0.995      0.817\n",
      "               0004-zz         85          6      0.873      0.833      0.955      0.664\n",
      "               0005-xz         85         18      0.817      0.889      0.869      0.606\n",
      "              0006-jtz         85          2      0.791          1      0.995      0.697\n",
      "          0007-x-wyzzx         85          6      0.851          1      0.995       0.79\n",
      "               0008-zc         85          7      0.928          1      0.995      0.638\n",
      "               0009-mt         85          4       0.72          1      0.895      0.684\n",
      "               0010-gz         85          5          1      0.927      0.995      0.544\n",
      "               0011-zw         85         17          1      0.696      0.861      0.627\n",
      "            0012-xszyz         85         11      0.865      0.909       0.93      0.579\n",
      "               0014-zz         85          3      0.796      0.667      0.732      0.593\n",
      "               0015-jc         85          8      0.929      0.875      0.944      0.754\n",
      "               0016-hx         85          4      0.881          1      0.995      0.849\n",
      "              0018-llz         85          6      0.861      0.833      0.955       0.63\n",
      "             0019-ydzy         85         11      0.581      0.727      0.475      0.294\n",
      "             0020-hfzy         85          5      0.745        0.6      0.631      0.296\n",
      "             0021-yzsb         85          5      0.795        0.6      0.615      0.493\n",
      "             0022-yzwg         85          5      0.861        0.6      0.656       0.45\n",
      "            0023-shzzy         85          3      0.798      0.667      0.806      0.358\n",
      "             0027-kkly         85          2      0.807          1      0.995      0.562\n",
      "               0029-tg         85          9      0.941          1      0.995      0.771\n",
      "               0030-gz         85          6      0.738        0.5      0.533      0.371\n",
      "             0031-rmjq         85         13      0.844      0.846      0.886      0.512\n",
      "               0032-jr         85          6      0.926          1      0.995      0.843\n",
      "               0034-yq         85          1          1          0          0          0\n",
      "              0035-mys         85         43      0.951      0.884      0.942      0.605\n",
      "          0038-frls-km         85          6          1      0.752      0.855       0.35\n",
      "          0039-frls-lj         85          2      0.676          1      0.995      0.797\n",
      "               0042-wb         85          3      0.818          1      0.995      0.932\n",
      "            0043-jrghs         85          2      0.637          1      0.995      0.648\n",
      "               0044-qq         85          6      0.875      0.667      0.809      0.629\n",
      "               0045-kl         85          3      0.857          1      0.995      0.816\n",
      "        0048-jmjt-qpmf         85          1       0.81          1      0.995      0.895\n",
      "        0049-jmjt-thhg         85          2      0.797          1      0.995      0.923\n",
      "        0051-jmjt-ftbt         85          5      0.897        0.8      0.962      0.703\n",
      "      0052-yymy-wxzlps         85          2      0.311          1      0.995      0.847\n",
      "      0053-yymy-hwcyjz         85          8          1      0.648      0.982      0.704\n",
      "              0057-kfk         85          2        0.8          1      0.995      0.995\n",
      "               0058-sm         85          2      0.809          1      0.995      0.863\n",
      "          0061-mnyz-sj         85          4      0.891          1      0.995      0.901\n",
      "        0062-mnyz-cjjl         85          5      0.777          1      0.995      0.681\n",
      "     0066-mnyz-xcrkwjh         85         29      0.932      0.948      0.932      0.622\n",
      "              0067-bfx         85         24      0.889      0.917      0.924      0.567\n",
      "              0068-bsd         85          5        0.9          1      0.995      0.834\n",
      "              0069-kgj         85         21      0.776          1      0.901      0.743\n",
      "Speed: 2.8ms preprocess, 78.6ms inference, 0.0ms loss, 1.5ms postprocess per image\n",
      "Results saved to \u001B[1mF:\\code\\workspace\\StarRail-YOLO\\ultralytics\\runs\\detect\\val4\u001B[0m\n"
     ]
    }
   ],
   "source": [
    "val_result = onnx_model.val(\n",
    "    data=ultralytics_utils.get_dataset_yaml_path(dataset_name),\n",
    "    imgsz=1088\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26c20a59-3435-473e-8ff7-48c2d82c5888",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
